<?php
// +--------------------------------------------------------------------------
// | ISDCE [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2017 载攸道 版权所有 <TEL:086-010-57209502>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * [ 公共模块 ]
 */
namespace Common\Controller;
use Think\Controller;

/**
 * 后台基类
 */
class AdminController extends Controller{
	
	//空操作
	public function _empty(){    
		
		
	}
	
	/* 初始化 */
    protected function _initialize(){
		
		//判断登录
		is_login();
		
		//判断用户权限
		$this->getUserAuthMenu();
		
		//获得横导航
		$this->getNav();
		//获得竖导航
		$this->getMenu();
		//获得配置
		$this->getConf();
		
		
    }
	
	/**
	 * 获取该用户是否有菜单权限
	 * @author 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
	 * @param  array $menus  判断用户菜单权限                 
	 */
	public function getUserAuthMenu(){
		
		//如果超级管理员则不考虑
		if(getUID()==1){
			
			return true;
		}
		//生成菜单
		$m_common_menu = M('common_menu');
		
		$url = MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME;
		
		//判断是不是插件
		$module = explode("/",$url);
		
		//如果是插件的话获得真实插件地址
		if($module[0]=='Home'){
			
			$addon = I();
		
			$addons = array_keys($addon);
		
			$addon_url = explode("_",$addons[0]);
			
			$url = $addon_url[0];
			
			$url = substr($url,1);
		}
		

		$mynav = getMyNav();
			
		foreach($mynav as $i=>$k){
			
			if($i==0){
				
				$id = $mynav[$i];
			}else{
				
				$id = $id.','. $mynav[$i];
			}
			
		}
		
		
		$auth_map['id'] = array('in',$id);
			
		$auth_map['url'] = array('like',"$url%");
		
		
		$data = $m_common_menu->where($auth_map)->find();
		
		if(!$data){
			
			if(IS_AJAX){
				
				//返回信息		
				$info = $info?$info:get_errcode(-3);

				$this->ajaxReturn($info);
			}else{
				
				$this->error('没有权限');
			}
			
			
		}
	}
	
	/* 获得页面基础配置 */
	public function getConf(){
		
		$item_name = getC('ITEM_NAME');
		$item_version = getC('ITEM_VERSION',2);
		$item_begin_time = getC('ITEM_BEGIN_TIME');
		$item_copyright = getC('ITEM_COPYRIGHT');
		$user_info = getUserInfo();
		
		$this->assign('item_name',$item_name);
		$this->assign('item_version',$item_version);
		$this->assign('item_begin_time',$item_begin_time);
		$this->assign('item_copyright',$item_copyright);
		$this->assign('user_info',$user_info);
		
    }
	
	/* 获得横导航 */
	public function getMenu(){
		
		$nav_id = getNavNID();
		
		$m_common_menu = M('common_menu');
		
		if(getUID()!=1){
			
			$mynav = getMyNav();
		
			foreach($mynav as $i=>$k){
				
				if($i==0){
					
					$id = $mynav[$i];
				}else{
					
					$id = $id.','. $mynav[$i];
				}
				
			}
			
			$map['id'] = array('in',$id);
			
		}
		
		
		$map['fmid'] = $nav_id['across'];
		$map['tier'] = 2;
		$map['status'] = 1;
		
		$menu = $m_common_menu->where($map)->order('sort desc')->select();
		
		$menu = $this->getNavStatus($menu,2);
		
		$this->assign('menu',$menu);
		
		
    }
	
	/* 获得横导航 */
	public function getNav(){
		
		$m_common_menu = M('common_menu');
		
		if(getUID()!=1){
			
			$mynav = getMyNav();
		
			foreach($mynav as $i=>$k){
				
				if($i==0){
					
					$id = $mynav[$i];
				}else{
					
					$id = $id.','. $mynav[$i];
				}
				
			}
			
			$map['id'] = array('in',$id);
			
		}
		
		
		
		$map['fmid'] = 0;
		$map['tier'] = 1;
		$map['status'] = 1;
		
		$nav = $m_common_menu->where($map)->order('sort desc')->select();
		
		$nav = $this->getNavStatus($nav,1);
		
		
		$this->assign('nav',$nav);
		
		
    }
	
	/* 获得导航状态 横导航 竖着导航 1是横导航 2是竖导航 3是行为*/ 
	function getNavStatus($nav,$type=1){
		
		$nav_id = getNavNID();
		
		
		if($type==1){
			
			foreach($nav as $i=>$k){
		
			
				if($nav[$i]['id'] == $nav_id['across']){
					
					$nav[$i]['active'] = "open";
				}
				
				//判断打开方式
				if($nav[$i]['target'] != 1){
					
					$nav[$i]['target'] = 'target="_blank"';
					
				}else{
					
					$nav[$i]['target'] = '';
				}

			}	
			
		}else if($type==2){
			
			foreach($nav as $i=>$k){
		
			
				if($nav[$i]['id'] == $nav_id['vertical']){
					
					$nav[$i]['active'] = "class='active'";
				}
				
				//判断打开方式
				if($nav[$i]['target'] != 1){
					
					$nav[$i]['target'] = 'target="_blank"';
					
				}else{
					
					$nav[$i]['target'] = '';
				}

			}
			
		}
		
		return $nav;

	}
	
	
}